// store/index.js
import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    authorityList: []
  },
  mutations: {
    changeAuthority(state, data) {
      state.authorityList = data
    },
    clearAuthority(state) {
      state.authorityList = []
    }
  },
  plugins: [
    createPersistedState({
      key: 'vuex',
      storage: window.sessionStorage, // 使用 sessionStorage
      paths: ['authorityList'], // 只持久化 authorityList
      
      // 可选：数据过滤，只保存需要的字段
      reducer: (state) => ({
        authorityList: state.authorityList.map(item => ({
          moduleId: item.moduleId,
          opeAdd: item.opeAdd,
          opeEdit: item.opeEdit,
          opeDel: item.opeDel,
          opeCheck: item.opeCheck
          // 不保存其他字段，减少存储量
        }))
      })
    })
  ]
})

export default store